Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add any() and all() methods to Array #50349

Merged
merged 1 commit into from
Jun 7, 2022

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented Jul 10, 2021

These can be used as faster, more convenient shorthands to using filter() + size().

This closes godotengine/godot-proposals#2926. reduce_right() wasn't implemented as it wasn't deemed useful enough in daily use cases.

Testing code

func _ready():
	print([5, 10, 0].any(func(v): return v > 5))  # true
	print([5, 5 , 0].any(func(v): return v > 5))  # false
	print([6, 10, 6].all(func(v): return v > 5))  # true
	print([6, 10, 0].all(func(v): return v > 5))  # false

@Calinou Calinou requested review from a team as code owners July 10, 2021 18:20
@Calinou Calinou added this to the 4.0 milestone Jul 10, 2021
@Calinou Calinou force-pushed the array-add-some-every branch 2 times, most recently from 569ecc3 to 5d412a6 Compare July 10, 2021 18:22
@kleonc
Copy link
Member

kleonc commented Jul 10, 2021

All great, just wondering about naming: was all/any considered as an alternative? Personally I'm used to all/any and that's what I opt for.
I've checked few languages:
all/any: C++, C#, Python, Rust, Java
every/some: JavaScript
I'm guessing more people is familiar with all/any.

@Calinou
Copy link
Member Author

Calinou commented Jul 10, 2021

All great, just wondering about naming: was all/any considered as an alternative? Personally I'm used to all/any and that's what I opt for.
I've checked few languages:
all/any: C++, C#, Python, Rust, Java
every/some: JavaScript
I'm guessing more people is familiar with all/any.

I have no strong preference, but I like how some and every have different lengths and start with a different letter. This makes them more distinguishable at a glance compared to any and all.

@floere
Copy link

floere commented Jul 10, 2021

All great, just wondering about naming: was all/any considered as an alternative? Personally I'm used to all/any and that's what I opt for.
I've checked few languages:
all/any: C++, C#, Python, Rust, Java
every/some: JavaScript
I'm guessing more people is familiar with all/any.

Ruby and Python also use any and all. I assume any is generally chosen because it’s usually used in questions with countable/uncountable nouns: “Are there any numbers that are larger than 5?”

@CedNaru
Copy link
Contributor

CedNaru commented Jul 11, 2021

I also agree that any/all is more to my taste as I'm used to those ones in Kotlin collections.

@fabriceci
Copy link
Contributor

As GdScript is a Python like syntax language, it's better to match when it's possible the Python syntax, especially for little thing like this.

@YuriSizov
Copy link
Contributor

YuriSizov commented Jul 11, 2021

@fabriceci It's not, it's just another indentation based language. Though in this case it wouldn't matter as there are several other popular languages that use the same method names.

@akien-mga
Copy link
Member

@pycbouh Well yes, GDScript is a Python-like language. We've often erred on the side of using the same names and syntaxes as Python whenever it was left to preference, unless the Python ones were really weird. GDScript is heavily inspired from the Godot API in C++, and sprinkled with bits of JavaScript and Rust.

Anyway, if it's "the whole world" vs "JavaScript", I suggest we follow "the whole world" here.

@YuriSizov
Copy link
Contributor

Yes, I guess sorry to fabriceci, I always read too strongly into people comparing the two.

@xix-xeaon
Copy link

xix-xeaon commented Jul 11, 2021

The Python any and all are not quite the same as the Javascript some and every. In Python they are global built-ins to which you pass an iterator, and they simply check if the items evaluate to True. In Javascript they are methods on the Array to which you pass a function for a custom evaluation. In Python you'd combine it with List comprehension to get something like the Javascript behavior.

I personally think List comprehension is clearer in this case but GDScript doesn't have it. I don't know about the other languages but GDScript is Python-like and so I think the proposed names some and every are better since the proposed functions do not work like the Python any and all. I think the Python any and all should also be implemented since they are useful - I've made such functions in GDScript several times.

In either case, if these functions are implemented they should be callable without passing a function, in which case the function essentially defaults to just bool evaluation.

@Calinou Calinou force-pushed the array-add-some-every branch 2 times, most recently from 5dc1592 to 063a5c2 Compare August 1, 2021 15:23
@Calinou Calinou changed the title Add some() and every() methods to Array Add any() and all() methods to Array Aug 1, 2021
@Calinou
Copy link
Member Author

Calinou commented Aug 1, 2021

I renamed some() and every() to any() and all() respectively.

@Calinou Calinou force-pushed the array-add-some-every branch 2 times, most recently from 9f405c3 to c9c3594 Compare August 1, 2021 20:25
@KoBeWi
Copy link
Member

KoBeWi commented Aug 2, 2021

Late to the discussion, but any(Callable) is the same as find(Callable) and all(Callable) is not find(reverse Callable), except we'd need a version of find() that takes a Callable (which would be more useful, because it'd also return the element/index).

These can be used as faster, more convenient shorthands to
using `filter()` + `size()`.
@Calinou Calinou force-pushed the array-add-some-every branch from c9c3594 to a98e31a Compare May 25, 2022 14:09
Copy link
Member

@akien-mga akien-mga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved in PR review meeting.

@akien-mga akien-mga merged commit 3f8d86b into godotengine:master Jun 7, 2022
@akien-mga
Copy link
Member

Thanks!

@Calinou Calinou deleted the array-add-some-every branch June 7, 2022 13:18
@me2beats
Copy link

me2beats commented Jun 17, 2022

wouldn't it be clearer to use is_ in the methods names like is_any, is_every
now all() can be confused with "get an array with all items" (if you don't look at the docs)

should we be consistent in that methods that return bool, start with is_

@kintrix007
Copy link

should we be consistent in that methods that return bool, start with is_

I do agree with that, but in this case it's either breaking a convention or being consistent with the is_ prefix. Since no languages that I know of call them is_any and is_every... And with is_every it would be breaking the conventions doubly, basically doing a mix and match of JavaScript's some/every and all the others' any/all...

So I would actually prefer if they stayed this way.

@me2beats
Copy link

is_every is not is_all because is_all would be are_all (according to English grammar, if I recall it right).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add some(), every() and reduce_right() methods to Array